// run: $exec < input
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <cstdlib>

int const maxm = 100007;
int a[maxm];
int n, m;

int main()
{
	std::cin >> n >> m;
	for (int i = 0; i < m; i++) std::cin >> a[i];
	if (m == 1) { std::cout << 0 << '\n'; return 0; }
	std::unordered_map<int, std::vector<int>> all;
	std::unordered_map<int, long long> delta;
	if (a[0] != a[1]) {
		delta[a[0]] += std::abs(a[0] - a[1]);
		all[a[0]].push_back(a[1]);
	}
	if (a[m - 1] != a[m - 2]) {
		delta[a[m - 1]] += std::abs(a[m - 1] - a[m - 2]);
		all[a[m - 1]].push_back(a[m - 2]);
	}
	for (int i = 1; i < m - 1; i++) {
		if (a[i] != a[i - 1]) {
			delta[a[i]] += std::abs(a[i - 1] - a[i]);
			all[a[i]].push_back(a[i - 1]);
		}
		if (a[i] != a[i + 1]) {
			delta[a[i]] += std::abs(a[i + 1] - a[i]);
			all[a[i]].push_back(a[i + 1]);
		}
	}
	long long ans = 0;
	for (int i = 1; i < m; i++) ans += std::abs(a[i] - a[i - 1]);
	long long tmp = ans;

	for (auto i : all) {
		auto & v = i.second;
		std::sort(v.begin(), v.end());
		int t = v[v.size() / 2];
		long long tt = 0;
		for (auto j : v) tt += std::abs(t - j);
		ans = std::min(ans, tmp - delta[i.first] + tt);
	}
	std::cout << ans << '\n';
}

